package com.badlogic.androidgames.framework.impl;

import java.io.IOException;

import com.badlogic.androidgames.framework.Audio;
import com.badlogic.androidgames.framework.Music;
import com.badlogic.androidgames.framework.Sound;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

/**
 * @deprecated:音频
 * @author frank
 * @date 2015年11月30日
 */
public class AndroidAudio implements Audio {
	/**资源管理**/
	AssetManager assets;
	/**声音池**/
	SoundPool soundPool;

	public AndroidAudio(Activity activity) {
		/** 设置音量控制流
		 	STREAM_MUSIC 音乐回放即媒体音量
		 	STREAM_RING 铃声
			STREAM_ALARM  警报
			STREAM_NOTIFICATION 窗口顶部状态栏通知声
			STREAM_SYSTEM  系统
			STREAM_VOICECALL 通话
			STREAM_DTMF 双音多频 **/
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		this.assets = activity.getAssets();
		/**最多同时播放的文件个数,音量控制流类型,采样率转化质量**/
		this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
	}
	
	@Override
	public Music newMusic(String filename) {
		try {
			/**资产文件描述符**/
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			return new AndroidMusic(assetDescriptor);
		} catch (IOException e) {
			throw new RuntimeException("无法加载音乐 '" + filename + "'");
		}
	}

	@Override
	public Sound newSound(String filename) {
		try {
			/**资产文件描述符**/
			AssetFileDescriptor assetDescriptor = assets.openFd(filename);
			/**load 加载 priority 优先级**/
			int soundId = soundPool.load(assetDescriptor, 0);
			return new AndroidSound(soundPool, soundId);
		} catch (IOException e) {
			throw new RuntimeException("无法加载声音 '" + filename + "'");
		}
		
	}

}
